package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author PTJ
 * @date 2024/11/05 09:34:53
 */
@Slf4j
@Service("userService")
public class UserService {
    @Resource
    private UserMapper userMapper;

    @Transactional(rollbackFor = Exception.class)
    public void testData1() {
        List<User> target = new ArrayList<>();
        List<Integer> list = Arrays.asList(1, 1, 1);
        for (Integer i : list) {
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getType, i);
            List<User> users = userMapper.selectList(wrapper);
            log.info("内存地址：{}", System.identityHashCode(users));
            log.info("source: {}", users);
            for (User user : users) {
                String name = String.format("%s-%s", user.getUsername(), "1");
                user.setUsername(name);
                target.add(user);
            }
        }
        log.info("end-----------------");
        log.info("target: {}", target);
    }
}
